<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>点击 DIV 换色</title>
    <link
      crossorigin="anonymous"
      href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
      rel="stylesheet"
    />
    <style>
      .item {
        width: 100px;
        height: 50px;
        border: solid 1px rgb(42, 156, 156);
        float: left;
        margin-right: 10px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h2 class="page-header">点击切换颜色</h2>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
    </div>
    <script>
      // 获取div元素对象
      let items = document.getElementsByClassName("item");
      // 遍历并绑定事件
      for (let i = 0; i < items.length; i++) {
        items[i].onclick = function () {
          // 修改当前元素背景色  this指向当前元素
          // this.style.background = 'pink';
          items[i].style.background = "pink";
          // 当i使用var定义时,当执行会回调函数时i为全局变量值为3

          console.log(i); // 0 1 2
          // 当i使用let定义时,i的作用域为块级作用域,当执行回调函数时i为初次循环的值
        };
      }
      // console.log(i);
      // var i=3
    </script>
  </body>
</html>
